var paginationTotal = 20;//记录总数
var isSearch = false;
var browseType = 'fold';    //浏览<{$lang.type}>
var isReload=false;
function search(val){
	if(val != undefined){
		$('#shipping_method_sch').val(val);
	}
	isSearch = true;
	paginationCurrentPage=1;
	initData(0);
}


function shiptype_search(val,obj){
	//$(".shiptype a").removeClass('sel');	
	//$(obj).addClass('sel');
	search(val);
}

function printFp()
{
        var tn=$("#orders_shipment").val();
		if(tn==''){return;}
		$.ajax({
	        type: "POST",
	        async: false,
	        dataType: "html",
	        url: "/order/order/draft-stop/code/"+tn,
	        success: function(html) {				
				var  pwin=window.open("a.html","print",'height=200, width=200, top=-200, left=-200, toolbar=no, menubar=no, scrollbars=yes,resizable=yes');   
	         	pwin.document.write(html);   
	          	pwin.document.close();             //这句很重要哦  
	         	//pwin.print();
	        }
		});
		
         
}

function draftbylist(orderid){
   var stoporderid = orderid;
	if($.trim(stoporderid)==""){
		$("#alert_msg").html("找不到<{$lang.orders_code}>!").dialog("open");
		return false;
	}
	 $.ajax({
	        type: "POST",
	        async: false,
	        dataType: "json",
	        url: "/order/order/draft-stop/" ,
	        data: {"stoporderid":stoporderid},
	        success: function(json) {
			
			   if(json.ask){
			        var  pwin=window.open("a.html","print",'height=200, width=200, top=-200, left=-200, toolbar=no, menubar=no, scrollbars=yes,resizable=yes');   
					pwin.document.write(json.html);   
					pwin.document.close();             //这句很重要哦  
					pwin.print();
					initData(0);
					//$("#alert_msg").html(json.result).dialog("open");
					//closeLoading();
			   }else{
			        alert(json.result);
			   }
			  
            }
	    });
	initData(0);
}

function draft(){
	var orders_shipment = $("#orders_shipment").val();
	if($.trim(orders_shipment)==""){
		$("#alert_msg").html("<{$lang.pls_input}><{$lang.orders_code}>/<{$lang.shipping_code}>").dialog("open");
		return false;
	}
	 $.ajax({
	        type: "POST",
	        async: false,
	        dataType: "json",
	        url: "/order/order/draft-stop/" ,
	        data: {"code":orders_shipment},
	        success: function(json) {
			if(json.ask){
			    var  pwin=window.open("a.html","print",'height=200, width=200, top=-200, left=-200, toolbar=no, menubar=no, scrollbars=yes,resizable=yes');   
	         	pwin.document.write(json.html);   
	          	pwin.document.close();             //这句很重要哦  
				pwin.print();
				initData(0);
	        	//$("#alert_msg").html(json.result).dialog("open");
	            //closeLoading();
			}else{
			    alert(json.result);
			}
			   
            }
	    });
	initData(0);
}
function loadData(page, pageSize) {
    var vForm = {};
    if (isSearch == true && isSearch != undefined) {
        vForm = $("#sForm").serialize();
        vForm+='&browsetype='+browseType;
    }
    vForm.browsetype = browseType;
    loading();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/order/order/stop-list/page/" + page + "/pageSize/" + pageSize,
        data: vForm,
        success: function(json) {
            closeLoading();
            var list = "";
            var trclass="";
            paginationTotal = json.count;
			var i = (page == 1 ? 1 : pageSize * (page - 1) + 1);
            if (json.ask == 0) {
                list = "<td colspan='13' class=\"center\">&nbsp;<{$lang.no_data}></td>";
            } else {
                //var i = page==1 ? 1 : pageSize * (page-1)+1;
                $.each(json.result, function(key, val) {
                    if ((key + 1) % 2 == 1 ) {
                        trclass='';
                    } else {
                        trclass='even-tr';
                    }
                    list += "<tr class="+trclass+">";
                    list += "<td><input type='checkbox' value='" + val.orders_code + "' id='orderids_" + val.orders_code + "' name='orderids[]'></td>";
					list += '<td>'+(key+1)+'</td>';
                    list += "<td><a href=\"javascript:detail('" + val.orders_code + "')\">" + val.orders_code + "</a></td>";
                    list += "<td>" + val.customer_order_code + "</td>";
                    list += "<td>" + val.customer_id + "</td>";
                    list += "<td>" + val.shipping_method + "</td>";
                    list += "<td>" + val.order_stop_date + "</td>";
                    list += "<td>" + val.add_time + "</td>";
                    list += "<td>" + val.print_time + "</td>";
					if(val.process_status<5&&val.process_status>0){
					    list += "<td>" + '待<{$lang.stop_order}>'+"/"+val.process_status_ch + "</td>";
					}else if(val.process_status==0&&val.process_status==0){
					    list += "<td>" + '已<{$lang.stop_order}>'+"/"+val.process_status_ch + "</td>";
					}
                    
					
                    list += "<td>" ;
					
                    if(val.process_status<5&&val.process_status>0){
                    	list += "<a href='javascript:;' onclick='draftbylist("+val.order_id+");'>" +	"<img src='/images/icon/delete.gif' title='<{$lang.stop_order}>'></a>" ;
                    }
                    list += "</td>";
                    list += "</tr>";
                   
                });
            }
            $("#loadData").html(list);
        }
    });
}

$(function() {
//    initData(0);
    $("[name=ckAll]").click(function() {
        if ($(this).is(':checked')) {
            $("[name='orderids[]']").attr('checked', true);
        } else {
            $("[name='orderids[]']").attr('checked', false);
        }
    });

    $('#order_form').dialog({
        autoOpen: false,
        width: 800,
        modal: true,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                submitOrder();
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            $('#orderForm')[0].reset();
        }
    });
    // <{$lang.delete}>
	$("#right_confirm").dialog({
		autoOpen : false,
		modal : true,
		height : 'auto',
		show : "slide",
		buttons : {
			'<{$lang.ok}>' : function() {
				$('#message').text("");
				loading();
				$.ajax({ // 这里使用到Jquery的ajax<{$lang.method}>
					type : "POST",
					async : false,
					dataType : "json",
					url : "/order/order/delete", // 请求的处理url
					data : {
						'order_code' : Uorder_code
					},
					success : function(json) {
						closeLoading();
						if (json.ask == 1) {// <{$lang.operation}><{$lang.success}>
							$('#alert_msg p').html("<{$lang.operation}><{$lang.success}>");
							$('#alert_msg').dialog('open');
							loadData(paginationCurrentPage, paginationPageSize);
						} else {
							$('#alert_msg p').text("<{$lang.operation}><{$lang.fail}>");
							$('#alert_msg').dialog('open');
						}
					}
				});
				$(this).dialog('close');
			},
			'<{$lang.cancel}>' : function() {
				$(this).dialog('close');
			}
		}
	});
	
	// 弹出<{$lang.tip}>
	$("#alert_msg").dialog({
		autoOpen : false,
		modal : true,
		width : 400,
		show : "slide",
		buttons : {
			'<{$lang.close}>' : function() {
				$(this).dialog('close');
			}
		}

	});
	//回车处理
	$("#code").keyup(function(e){
		var key = e.which;
		if(key==13){
			search();
		}
	});
	//回车处理
	$("#orders_shipment").keyup(function(e){
		var key = e.which;
		if(key==13){
			draft();
		}
	});
	
	
});

//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
                $(this).dialog("close");
            }
        }
    });
}
//<{$lang.order}><{$lang.detail}>
function detail(orders_code) {
    var url = '/order/order/detail/orders_code/' + orders_code;
    window.open(url);
}
//<{$lang.product}><{$lang.detail}>

function proDetails(id) {
    if (isNaN(id) || !id) {
        return false;
    }
    var url = '/order/order/prodetail/id/' + id;
    window.parent.$('<iframe id="ifr_order_product_detail" name="ifr_order_product_detail" src="' + url + '" />').dialog({
        title: '<{$lang.order}><{$lang.product}><{$lang.detail}><{$lang.msg}>',
        autoOpen: true,
        width: 900,
        modal: false,
        resizable: false,
        position:'top',
        autoResize: true,
        close:function() {
            $(this).remove();
            if (isReload) {
                window.location.reload();
                isReload = false;
            }
        },
        buttons: {
            'Close/<{$lang.close}>': function() {
                $(this).remove();
            }
        }
    }).width(875);
}



function del(order_id,ws_code,process_status){
	Uorder_code = ws_code;
	var text = process_status==1?("<{$lang.order_back_to_delete}>："+ws_code+" <{$lang.delete_sure}>?"):("<{$lang.order_back_to_pickup}>："+ws_code+",<{$lang.delete_sure}>?");
	$("#right_confirm p").html(text+',<{$lang.ok}><{$lang.operation}>？ ');
	$("#right_confirm").dialog('open');
}